package com.mezizy.ifaner;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.laohuai.appdemo.customui.MyListView;
import com.mezizy.ifaner.global.Config;
import com.mezizy.ifaner.global.Global;
import com.mezizy.ifaner.global.com.mezizy.ifaner.web.data.Order;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


public class OrdersTab extends Fragment {

    public static MyListView listViewOrder;

    public static zSimpleAdapter simpleAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
//        return inflater.inflate(R.layout.tab_orders, container, false);
        View view = inflater.inflate(R.layout.tab_orders, container, false);
        listViewOrder = (MyListView) view.findViewById(R.id.listViewOrder);

        listViewOrder.setOnRefreshListener(new MyListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new OrdersAsync().execute();

            }
        });
//        new OrdersAsync().execute();

        return view;
    }

    public static class zSimpleAdapter extends SimpleAdapter {

        /**
         * Constructor
         *
         * @param context  The context where the View associated with this SimpleAdapter is running
         * @param data     A List of Maps. Each entry in the List corresponds to one row in the list. The
         *                 Maps contain the data for each row, and should include all the entries specified in
         *                 "from"
         * @param resource Resource identifier of a view layout that defines the views for this list
         *                 item. The layout file should include at least those named views defined in "to"
         * @param from     A list of column names that will be added to the Map associated with each
         *                 item.
         * @param to       The views that should display column in the "from" parameter. These should all be
         *                 TextViews. The first N views in this list are given the values of the first N columns
         */
        public zSimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
            super(context, data, resource, from, to);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            final Map<String, Object> orderMap = (Map<String, Object>) getItem(position);

            TextView txtOrderState = (TextView) view.findViewById(R.id.txtOrderState);
            int state = (int) ((Map<String, Object>) getItem(position)).get("orderState");
            txtOrderState.setText(Global.getStateS(state));

            view.findViewById(R.id.layoutOrder).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String orderId = (String) orderMap.get("orderId");
                    Order order = ((Order) orderMap.get("order"));
                    Intent intent = new Intent(MainActivity.getInstance(), OrderDetailActivity.class).putExtra("orderId", orderId);
                    intent.putExtra("order", order);

                    MainActivity.getInstance().startActivity(intent);
                }
            });
            return view;
        }
    }
     public static void startAsync() {
        new OrdersAsync().execute();
    }

    static class OrdersAsync extends AsyncTask<LinkedList<Order>, LinkedList<Order>, LinkedList<Order>> {

        @Override
        protected void onPostExecute(LinkedList<Order> orderList) {
            LinkedList<Map<String, Object>> list = new LinkedList<>();
            //TODO  新订单 适配
            for (Order order : orderList) {
                LinkedHashMap<String, Object> map = new LinkedHashMap<>();
                map.put("storeName", order.getMerchantName());
                map.put("date", order.getCreateTime());
                map.put("price", "NT " + order.getAmount());
                map.put("orderState", order.getTradeState());
                map.put("orderId", order.getTradeId());
                map.put("order", order);
                list.add(map);
            }

            simpleAdapter = new zSimpleAdapter(MainActivity.getInstance(), list, R.layout.list_order, new String[]{"storeName", "date", "price"}, new int[]{R.id.txtOrderStoreName, R.id.txtOrderDate, R.id.txtOrderPrice});

            listViewOrder.setAdapter(simpleAdapter);
            listViewOrder.onRefreshComplete();
            Global.dismissProgress(MainActivity.getInstance());
            super.onPostExecute(orderList);
        }

        @Override
        protected LinkedList<Order> doInBackground(LinkedList<Order>... params) {
            Global.showProgress(MainActivity.getInstance(), "加載中");
            LinkedList<Order> list = new LinkedList<>();

            LinkedHashMap<String, Order> map = Global.getOrderListUser();
            if (map == null || map.entrySet() == null) {
                return list;
            }
            for (Map.Entry<String, Order> entry : map.entrySet()) {
                Order order = entry.getValue();
                list.add(order);
            }

            return list;
        }
    }

}
